package com.huaxin.hxcloud.find;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.Constants;
import com.huaxin.hxcloud.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hebing on 2017/5/11.
 */

public class GridViewAdapter extends BaseAdapter {
    private int widthPixels;
    private List<String> lists;
    private Context context;

    public GridViewAdapter(List<String> lists,Context context) {
        this.lists = lists;
        this.context = context;
        this.widthPixels = context.getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public int getCount() {
        int count=0;
        if (lists==null)
            return count;
        count=lists.size();
        if (count>3)
            return 3;
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_top_info_2, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthPixels / 3 - 10, widthPixels / 3 - 10);
        if ((position + 1) % 3 == 1 || position == 0) {
            params.setMargins(15, 5, 0, 5);
        } else {
            params.setMargins(5, 5, 0, 5);
        }
        holder.ci1.setLayoutParams(params);
        Glide.with(context).load(lists.get(position)).apply(new RequestOptions().placeholder(Constants
                .DEFAULT_LOADING_IMG)).into(holder.ci1);

       /* Glide.with(context).load(lists.get(position)).placeholder(Constants
                .DEFAULT_LOADING_IMG).into(holder.ci1);*/
        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.find_ci1)
        ImageView ci1;

        public ViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }
}